#pragma once
#include "Log.hpp"
#include "data.hpp"

namespace my_cloud{
    class HotManager{
    private:
        std::string _back_dir;
        std::string _pack_dir;
        std::string _pack_suffix;
        int _hot_time;
        bool HotJudge(const std::string filename){
            FileUtil fu(filename);
            time_t last_time = fu.FileATime();
            time_t cur_time = time(NULL);
            if(cur_time - last_time > _hot_time){
                return true;
            }
            return false;
        }
    public:
        HotManager(){
            auto conf = Config::GetInstance();
            _back_dir = conf->GetBackDir();
            _pack_dir = conf->GetPackDir();
            _pack_suffix = conf->GetPackFileSuffix();
            _hot_time = conf->GetHotTime();
            FileUtil(_back_dir).CreateDir();
            FileUtil(_pack_dir).CreateDir();
        }
        bool RunModule(){
            auto data = DataManager::GetInstance();
            FileUtil back_fu(_back_dir);
            while(1){
                std::vector<std::string> arry;
                back_fu.ScanDir(arry);
                for(int i = 0; i < arry.size(); ++i){
                    if(HotJudge(arry[i]) == false){
                        continue;
                    }
                    BackupInfo info;
                    if(data->GetOneByRealPath(arry[i], &info) == false){
                        info.NewBackupInfo(arry[i]);
                    }
                    FileUtil fu(arry[i]);
                    fu.Compress(info._pack_path);
                    fu.Remove();
                    info._pack_flag = true;
                    data->Updata(info);
                    LOG(INFO, arry[i] + " ComPress OK!");
                }
                usleep(1000);
            }
            return true;
        }
    };
}